<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>shardingtestcase.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_testing_shardingtestcase.js.html">shardingtestcase.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utility for sharding tests.
<a name="line17"></a> *
<a name="line18"></a> * Usage instructions:
<a name="line19"></a> * &lt;ol&gt;
<a name="line20"></a> *   &lt;li&gt;Instead of writing your large test in foo_test.html, write it in
<a name="line21"></a> * foo_test_template.html&lt;/li&gt;
<a name="line22"></a> *   &lt;li&gt;Add a call to {@code goog.testing.ShardingTestCase.shardByFileName()}
<a name="line23"></a> * near the top of your test, before any test cases or setup methods.&lt;/li&gt;
<a name="line24"></a> *   &lt;li&gt;Symlink foo_test_template.html into different sharded test files
<a name="line25"></a> * named foo_1of4_test.html, foo_2of4_test.html, etc, using `ln -s`.&lt;/li&gt;
<a name="line26"></a> *   &lt;li&gt;Add the symlinks as foo_1of4_test.html.
<a name="line27"></a> *       In perforce, run the command `g4 add foo_1of4_test.html` followed
<a name="line28"></a> * by `g4 reopen -t symlink foo_1of4_test.html` so that perforce treats the file
<a name="line29"></a> * as a symlink
<a name="line30"></a> *   &lt;/li&gt;
<a name="line31"></a> * &lt;/ol&gt;
<a name="line32"></a> *
<a name="line33"></a> */
<a name="line34"></a>
<a name="line35"></a>goog.provide(&#39;goog.testing.ShardingTestCase&#39;);
<a name="line36"></a>
<a name="line37"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line38"></a>goog.require(&#39;goog.testing.TestCase&#39;);
<a name="line39"></a>
<a name="line40"></a>/**
<a name="line41"></a> * A test case that runs tests in per-file shards.
<a name="line42"></a> * @param {number} shardIndex Shard index for this page,
<a name="line43"></a> *     &lt;strong&gt;1-indexed&lt;/strong&gt;.
<a name="line44"></a> * @param {number} numShards Number of shards to split up test cases into.
<a name="line45"></a> * @extends {goog.testing.TestCase}
<a name="line46"></a> * @constructor
<a name="line47"></a> */
<a name="line48"></a>goog.testing.ShardingTestCase = function(shardIndex, numShards, opt_name) {
<a name="line49"></a>  goog.base(this, opt_name);
<a name="line50"></a>
<a name="line51"></a>  goog.asserts.assert(shardIndex &gt; 0, &#39;Shard index should be positive&#39;);
<a name="line52"></a>  goog.asserts.assert(numShards &gt; 0, &#39;Number of shards should be positive&#39;);
<a name="line53"></a>  goog.asserts.assert(shardIndex &lt;= numShards,
<a name="line54"></a>      &#39;Shard index out of bounds&#39;);
<a name="line55"></a>
<a name="line56"></a>  /**
<a name="line57"></a>   * @type {number}
<a name="line58"></a>   * @private
<a name="line59"></a>   */
<a name="line60"></a>  this.shardIndex_ = shardIndex;
<a name="line61"></a>
<a name="line62"></a>  /**
<a name="line63"></a>   * @type {number}
<a name="line64"></a>   * @private
<a name="line65"></a>   */
<a name="line66"></a>  this.numShards_ = numShards;
<a name="line67"></a>};
<a name="line68"></a>goog.inherits(goog.testing.ShardingTestCase, goog.testing.TestCase);
<a name="line69"></a>
<a name="line70"></a>
<a name="line71"></a>/**
<a name="line72"></a> * Whether we&#39;ve actually partitioned the tests yet. We may execute twice
<a name="line73"></a> * (&#39;Run again without reloading&#39;) without failing.
<a name="line74"></a> * @type {boolean}
<a name="line75"></a> * @private
<a name="line76"></a> */
<a name="line77"></a>goog.testing.ShardingTestCase.prototype.sharded_ = false;
<a name="line78"></a>
<a name="line79"></a>
<a name="line80"></a>/**
<a name="line81"></a> * Installs a runTests global function that goog.testing.JsUnit will use to
<a name="line82"></a> * run tests, which will run a single shard of the tests present on the page.
<a name="line83"></a> * @override
<a name="line84"></a> */
<a name="line85"></a>goog.testing.ShardingTestCase.prototype.runTests = function() {
<a name="line86"></a>  if (!this.sharded_) {
<a name="line87"></a>    var numTests = this.getCount();
<a name="line88"></a>    goog.asserts.assert(numTests &gt;= this.numShards_,
<a name="line89"></a>        &#39;Must have at least as many tests as shards!&#39;);
<a name="line90"></a>    var shardSize = Math.ceil(numTests / this.numShards_);
<a name="line91"></a>    var startIndex = (this.shardIndex_ - 1) * shardSize;
<a name="line92"></a>    var endIndex = startIndex + shardSize;
<a name="line93"></a>    goog.asserts.assert(this.order == goog.testing.TestCase.Order.SORTED,
<a name="line94"></a>        &#39;Only SORTED order is allowed for sharded tests&#39;);
<a name="line95"></a>    this.setTests(this.getTests().slice(startIndex, endIndex));
<a name="line96"></a>    this.sharded_ = true;
<a name="line97"></a>  }
<a name="line98"></a>
<a name="line99"></a>  // Call original runTests method to execute the tests.
<a name="line100"></a>  goog.base(this, &#39;runTests&#39;);
<a name="line101"></a>};
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * Shards tests based on the test filename. Assumes that the filename is
<a name="line106"></a> * formatted like &#39;foo_1of5_test.html&#39;.
<a name="line107"></a> * @param {string=} opt_name A descriptive name for the test case.
<a name="line108"></a> */
<a name="line109"></a>goog.testing.ShardingTestCase.shardByFileName = function(opt_name) {
<a name="line110"></a>  var path = window.location.pathname;
<a name="line111"></a>  var shardMatch = path.match(/_(\d+)of(\d+)_test\.html/);
<a name="line112"></a>  goog.asserts.assert(shardMatch,
<a name="line113"></a>      &#39;Filename must be of the form &quot;foo_1of5_test.html&quot;&#39;);
<a name="line114"></a>  var shardIndex = parseInt(shardMatch[1], 10);
<a name="line115"></a>  var numShards = parseInt(shardMatch[2], 10);
<a name="line116"></a>
<a name="line117"></a>  var testCase = new goog.testing.ShardingTestCase(
<a name="line118"></a>      shardIndex, numShards, opt_name);
<a name="line119"></a>  goog.testing.TestCase.initializeTestRunner(testCase);
<a name="line120"></a>};
</pre>


</body>
</html>
